const lodash = require('lodash');
const view = {

    init(tablename, listSQL, querySQL, componentNames, componentsDef, componentsHtml, requiredSQL,primaryKey) {
        let camelCaseTableName = lodash.camelCase(tablename);
        let camelCaseTableNameFirst = lodash.upperFirst(camelCaseTableName);
        let modelName = camelCaseTableName + "Model";
        let page = `
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Drawer, Form, Button, Col, Row, ${componentNames.join(",")} } from 'antd';
import util from 'utils/util';
${componentsDef}

// 与model建立连接
@connect(({ ${modelName} }) => ({
    editShowVisible: ${modelName}.editShowVisible,
    editData: ${modelName}.editData
}))
@Form.create()
class Edit extends PureComponent {

    constructor(props) {
        super(props);
        this.state = {
            //用于重新加载数据项
            fromKey: Math.random()
        };
    }

    componentDidMount() {
    }

    onClose = () => {
        const { dispatch } = this.props;
        dispatch({
            type: '${modelName}/updateState',
            payload: { editShowVisible: false },
        });
    };

    submitModel = (params) => {
        const { dispatch,editData } = this.props;
        //加入主键
        params.subData.${primaryKey.t_column_name} = editData.${primaryKey.t_column_name};
        let that = this;
        dispatch({
            type: "${modelName}/editRowData",
            payload: params,
            callback: (res) => {
                if (res) {
                    that.props.form.resetFields();
                    that.setState({ fromKey: Math.random() });
                }
            }
        });
    }

    handleSubmit = e => {
        e.preventDefault();
        this.props.form.validateFields((errors, values) => {
            if (!errors) {
                console.log("提交数据", values)
                let subData = values;
                //向model传递数据
                this.submitModel({ subData });     
            }
        });
    };

    render() {
        const { getFieldDecorator } = this.props.form;
        const { editShowVisible,editData } = this.props;
        const { fromKey } = this.state;
        const { onClose,handleSubmit } = this;
        return (
            <div>
                <Drawer
                    title="编辑"
                    width={720}
                    onClose={onClose}
                    visible={editShowVisible}
                >
                    <Form layout="vertical" key={ fromKey }>
                        ${componentsHtml}
                        <div className="drawerBottombutton">
                            <Button onClick={handleSubmit} type="primary" style={{ marginRight: 8 }}>提交</Button>
                            <Button onClick={onClose}>取消</Button>
                        </div>
                    </Form>
                </Drawer>
            </div>
        );
    }
}

export default Edit;`;
        return page; 
    }

}

module.exports = view;